<template>
    <div class="addAddress">
        <!-- 头部导航 -->
        <div class="header">
            <van-nav-bar
                title="不凡"
                left-text="返回"
                left-arrow
                @click-left="onClickLeft"
            />
        </div>
        <!-- 添加地址 -->
        <div class="content">
            <van-address-edit
                :area-list="areaList"
                show-delete
                show-set-default
                :area-columns-placeholder="['请选择', '请选择', '请选择']"
                :area-placeholder="defaultArea"
                :address-info="defaultAddress"
                @save="onSave"
                @delete="onDelete"
            />
        </div>
    </div>
</template>

<script>
import areaList from '@/assets/lib/json/areaList'
import { saveAddressInfo, getAddressListInfo, removeAddressInfo, addressInfo } from '@/api/my/address/index'
import { Toast } from 'vant';

    export default {
        data () {
            return {
                areaList,
                openId: localStorage.getItem('openId'),
                addressList: '',
                defaultAddress: {},
                defaultArea: ''
            }
        },
        methods: {
            // 返回
            onClickLeft() {
                this.$router.go(-1);
            },
            // 点击保存
            onSave(content) {
                var obj = {
                    address: content.province + ' ' + content.city + ' ' + content.county,
                    checked: content.isDefault,
                    detailadress: content.addressDetail,
                    openId: this.openId,
                    telNumber: content.tel,
                    userName: content.name,
                }
                // 如果是修改
                if (this.$route.query.id) {
                    obj.addressId = this.$route.query.id;
                }
                // 提交地址到服务器
                saveAddressInfo(obj)
                .then(res => {
                    console.log(res);
                    // 返回上一级
                    this.onClickLeft()
                })
            },
            // 点击删除
            onDelete() {
                // 如果是修改数据
                if (this.$route.query.id) {
                    removeAddressInfo({ id: this.$route.query.id })
                    .then(res => {
                        console.log('删除', res);
                        // 返回上一级
                        this.onClickLeft()
                    })
                } else {
                    Toast.fail('请先添加地址！');
                }
            },
        },
        created() {
            // 如果是修改
            if (this.$route.query.id) {
                addressInfo({ id: this.$route.query.id })
                .then(res => {
                    console.log(res);
                    // 设置默认的地址信息
                    var obj = {
                        id: res.data.id,
                        name: res.data.name,
                        tel: res.data.mobile,
                        addressDetail: res.data.address_detail,
                        isDefault: res.data.is_default == 1 ? true : false,
                    }
                    // 默认信息
                    this.defaultAddress = obj
                    // 默认区域
                    this.defaultArea = res.data.address;
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .addAddress{
        width: 100%;
        .header{
            background: #fff;
        }
    }
</style>
